#pragma once
#include "p2.h"
#include "OrderReqInfo.h"
#include "OrderInfo.h"
#include "GxOrderHandlerEventSink.h"
#include "GxOrderEventSink.h"
#include "GxFillEventSink.h"
#include "GxConfirmEventSink.h"
#include "GxPositionEventSink.h"
#include "Global.h"
#include "OrderEventSink.h"
#include "ConfirmEventSink.h"
#include "PositionEventSink.h"

/*
class GxOrderHandlerEventSink;
class GxOrderEventSink;
class GxFillEventSink;
class GxConfirmEventSink;
class GxPositionEventSink;
*/
class COrderParam;
class CMainOrderPanel;
class CAccountManager;
class CAccountInfo;
class COrderManager
{
public:

	CAccountInfo*    m_pCurAccount;
	CMainOrderPanel* m_pMainWnd;
	CAccountManager* m_pAccountMgr;
	COrderManager();
	COrderManager(IGxServer& a_Server);
	~COrderManager();

	void Init(IGxServer& a_Server, CMainOrderPanel* a_pMainWnd);

	void InitOrders();
	void InitOrders(CString a_AccountCode, CString a_SymbolCode);
	void InitOrdersByAccount(CString a_AccountCode, CString a_SymbolCode);

	void AdviseAllEvent();
	void UnadviseAllEvent();

	void NewOrder(OrderReqParam& orderReqParam);
	void ModifyOrder(OrderReqParam& orderReqParam);
	void CancelOrder(OrderReqParam& orderReqParam);

	void ModifyOrder(OrderReqParam& orderReqParam, OrderVector* a_OrderVector);
	void CancelOrder(OrderReqParam& orderReqParam, OrderVector* a_OrderVector);

	void ModifyOrder(IPositionType a_PT, CString a_OldPrice, CString a_OldAmount, CString a_NewPrice);
	void CancelOrder(IPositionType a_PT, CString a_OldPrice, CString a_OldAmount);

	void AdviseConfirmEvent();
	void UnadviseConfirmEvent();
	void OnNewConfirm(IGxConfirm a_GxConfirm);
	
	void AdviseFillEvent();
	void UnadviseFillEvent();
	void OnNewFill(LPDISPATCH a_GxFill);
	void OnFillUpdated(LPDISPATCH a_GxFill);

	void AdviseOrderEvent();
	void UnadviseOrderEvent();
	void OnNewOrder(IGxOrder a_GxOrder);
	void OnOrderUpdated(IGxOrder a_GxOrder);

	void AdviseOrderReqEvent();
	void UnadviseOrderReqEvent();
	void OnStateChanged(IGxOrderReq a_GxOrderReq);
	void OnStateChanged();

	void AdvisePositionEvent();
	void UnadvisePositionEvent();

	void OnNewPosition(LPDISPATCH a_GxPosition);
	void OnPositionUpdated(LPDISPATCH a_GxPosition);
	void OnEvalPLUpdated(LPDISPATCH a_GxPosition);

	void OnNewPosition(IGxPosition a_GxPosition);
	void OnPositionUpdated(IGxPosition a_GxPosition);
	void OnEvalPLUpdated(IGxPosition a_GxPosition);

	mapAccountOrder m_AccOdrMap;



	mapOrderReq m_OrderReqMap;
	mapOrder    m_OrderMap;
	mapOrder* GetOrderMap()
	{
		return &m_OrderMap;
	}
	COrderReqInfo* AddOrderReq(long a_AcptNm);
	COrderReqInfo* FindOrderReq(long a_AcptNm);
	COrderInfo* AddOrder(long a_AcptNm);
	COrderInfo* FindOrder(long a_AcptNm);
	COrderInfo* FindPendingOrder(double a_Price, IPositionType a_PositionType, IOrderStateType a_OrderState);

	COrderInfo* AddOrder(CString a_Account, long a_AcptNm);
	COrderInfo* FindOrder(CString a_Account, long a_AcptNm);
	COrderInfo* FindPendingOrder(CString a_Account, double a_Price, IPositionType a_PositionType, IOrderStateType a_OrderState);


	void AdviseEvent();
	void UnadviseEvent();

	COrderEventSink m_OrderSink;
	COrderEventSink m_OrderReqSink;
	CConfirmEventSink m_ConfirmSink;

private:
	COrderInfo* AddOrder(mapOrder& a_OrderMap, long a_AcptNm);
	COrderInfo* FindOrder(mapOrder& a_MapOrder, long a_AcptNm);
	IGxServer m_Server;
	GxOrderHandlerEventSink m_OrderHandlerEventSink;
	GxOrderEventSink m_OrderEventSink;
	GxConfirmEventSink m_ConfirmEventSink;
	GxFillEventSink m_FillEventSink;
	GxPositionEventSink m_PositionEventSink;


	//CPositionEventSink m_PositionSink;
};

